﻿<?php
session_start();
include "connectsql.php";
if (isset($_REQUEST["reg"])) {
	$regflag = $_REQUEST["reg"];
	date_default_timezone_set('PRC');
} else {
	die (ECHO_NO_REGFLAG);
}

if (($regflag >= FLAG_LOGIN && $regflag <= FLAG_DEL) || ($regflag >= FLAG_RET_SIZE && $regflag <= FLAG_SET_TIME)) {
	if (isset($_SESSION["ceo"]) && $_SESSION["ceo"] == true) {
		$manager = $_SESSION["manager"];
	} else {
		die (ECHO_LOGIN_OVER_TIME);	
	}
	
	if ($regflag == FLAG_LOGIN) {
		if (isset($_REQUEST['name'])) {
			$username = $_REQUEST['name'];
			if ($username == "")
				die (ECHO_NO_INPUTNAME);	
		} else {
			die (ECHO_NO_INPUTNAME);	
		}

		$limittime = time() - 7776000;
		$cmdtime = date('Y-m-d_H:i:s', $limittime);	
		$sql = "select * from ".SetDB_CMD_TAB($manager)." where `username` = '".$username."' and `date` >= '".$cmdtime."' order by `id`";
		if ($query = mysqli_query($link, $sql)) {
			$showtxt = "machineshowcmdstart:";
			$num = 0;
			
			while ($row = mysqli_fetch_assoc($query)) {
				$id = $row["id"];
				$date = $row["date"];
				$explain = $row["explain"];
				$state = $row["state"];
				$extrainfo = $row["extrainfo"];
				if ($num != 0) {
					$showtxt .= ";;";
				}
				if ($extrainfo == "")
					$extrainfo = "0";		
				$showtxt .= $date.",,".$explain.",,".$state.",,".$id.",,".$extrainfo;
				$num++;
			}
			$showtxt .= ":machineshowcmdover";
			echo $showtxt;
			mysqli_free_result($query);
		} else {
			die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		}
	} else if ($regflag == FLAG_CHANGE) {
		if (isset($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
			if ($id == "")
				die (ECHO_NO_ID);
		} else {
			die (ECHO_NO_ID);	
		}

		if (isset($_REQUEST['state'])) {
			$state = $_REQUEST['state'];
			if ($state == URL_TYPE_HAS_READ_NO_DEL) {
				$state = URL_TYPE_HAS_READ_HAS_DEL;
			} else {
				die (ECHO_NO_STATE);	
			}
		} else {
			die (ECHO_NO_STATE);
		}	
		
		$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".$state."' where `id` = '".$id."'";
		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
		
		echo ECHO_OK;
	} else if ($regflag == FLAG_REG) {
		if (isset($_REQUEST['date'])) {
			$date = $_REQUEST['date'];
			if ($date == "")
				die (ECHO_NO_DATE);	
		} else {
			die (ECHO_NO_DATE);	
		}

		if (isset($_REQUEST['name'])) {
			$username = $_REQUEST['name'];
			if ($username == "")
				die (ECHO_NO_INPUTNAME);	
		} else {
			die (ECHO_NO_INPUTNAME);	
		}
		
		if (isset($_REQUEST['cmd'])) {
			$cmd = $_REQUEST['cmd'];
			if ($cmd == "")
				die (ECHO_NO_CMD);	
		} else {
			die (ECHO_NO_CMD);	
		}

		if (substr($cmd, 0, 13) == "cmd:setalarm:") {
			$alarm = substr($cmd, -48);
			$sql = "update ".SetDB_USER_TAB($manager)." set `alarm` = '".$alarm."' where `username` = '".$username."'";		 							
			mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);						
		}

		if (isset($_REQUEST['explain'])) {
			$explain = $_REQUEST['explain'];
		} else {
			$explain = "";
		}
	
		if (isset($_REQUEST['state'])) {
			$state = $_REQUEST['state'];
			if (($state == CMD_TYPE_NO_READ || $state == URL_TYPE_NO_READ || $state == CMD_TYPE_HAS_READ || $state == URL_TYPE_HAS_READ_NO_DEL || $state == URL_TYPE_HAS_READ_HAS_DEL) == false) {
				die (ECHO_NO_STATE);
			}
		} else {
			die (ECHO_NO_STATE);
		}
		
		if (isset($_REQUEST['extra'])) {
			$extrainfo = $_REQUEST['extra'];
			if ($extrainfo == "") {
				$extrainfo = "0";
			}
		} else {
			$extrainfo = "0";
		}		
		
		if (isset($_REQUEST['clearfile'])) {
			$clearfile = $_REQUEST['clearfile'];
			if ($clearfile == "1") {
	  		$sql = "update ".SetDB_USER_TAB($manager)." set `terminalfile` = '0' where `username` = '".$username."'";		 							
				mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);				
			}
		}
		
		$modexml = "";
		$playlistxml = "";
		if (isset($_REQUEST['modexml'])) {
			$modexml = $_REQUEST['modexml'];
		}
		if (isset($_REQUEST['playlistxml'])) {
			$playlistxml = $_REQUEST['playlistxml'];
		}
		if ($modexml != "" || $playlistxml != "") {
			if ($modexml != "" && $playlistxml == "") {
				$sql = "update ".SetDB_USER_TAB($manager)." set `modexml` = '".$modexml."', `downloadper` = '0' where `username` = '".$username."'";
				mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			} else if ($modexml == "" && $playlistxml != "") {
				$sql = "update ".SetDB_USER_TAB($manager)." set `playlistxml` = '".$playlistxml."', `downloadper` = '0' where `username` = '".$username."'";
				mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			} else if ($modexml != "" && $playlistxml != "") {
				$sql = "update ".SetDB_USER_TAB($manager)." set `modexml` = '".$modexml."', `playlistxml` = '".$playlistxml."', `downloadper` = '0' where `username` = '".$username."'";
				mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			}
			if ($modexml != "") {
				$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".URL_TYPE_HAS_READ_INVALID."' where `username` = '".$username."' and `state` = '".URL_TYPE_NO_READ."' and `type` = '".FILE_TYPE_MODEXML."'";
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			}
			if ($playlistxml != "") {
				$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".URL_TYPE_HAS_READ_INVALID."' where `username` = '".$username."' and `state` = '".URL_TYPE_NO_READ."' and `type` = '".FILE_TYPE_PLAYLISTXML."'";
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);						
			}
		}

		$type = FILE_TYPE_NONE;		
		if (isset($_REQUEST['updateimg'])) {
			$updateimg = $_REQUEST['updateimg'];
			if ($updateimg == "") {
				die (ECHO_ERROR);				
			}
			$type = FILE_TYPE_UPDATEIMG;
			$sql = "update ".SetDB_USER_TAB($manager)." set `downloadper` = '0' where `username` = '".$username."'";
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);		
			$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".URL_TYPE_HAS_READ_INVALID."' where `username` = '".$username."' and `state` = '".URL_TYPE_NO_READ."' and `type` = '".$type."'";
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);	
		}

		if (substr($cmd, 0, 4) == "cmd:") {
			if ($cmd == "cmd:0" || $cmd == "cmd:1") {
				$type = CMD_TYPE_POWEROFFON;
			} else if ($cmd == "cmd:2") {
				$type = CMD_TYPE_MUTE;
			} else if ($cmd == "cmd:3") {
				$type = CMD_TYPE_CLEAR_GPRS;
			} else if (substr($cmd, 0, 6) == "cmd:4:") {
				$type = CMD_TYPE_VOLUME;
			} else if (substr($cmd, 0, 13) == "cmd:setalarm:") {
				$type = CMD_TYPE_ALARM;
			} else if (substr($cmd, 0, 8) == "cmd:path") {
				$type = CMD_TYPE_PATH;
			} else if (substr($cmd, 0, 7) == "cmd:3g:") {
				$type = CMD_TYPE_LIMIT_GPRS;
			} else if (substr($cmd, 0, 10) == "cmd:audio:") {
				$type = CMD_TYPE_LIMIT_VOLUME;
			}
			if ($type == CMD_TYPE_POWEROFFON || $type == CMD_TYPE_MUTE || $type == CMD_TYPE_CLEAR_GPRS || $type == CMD_TYPE_VOLUME || $type == CMD_TYPE_ALARM || $type == CMD_TYPE_PATH) {
				$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".CMD_TYPE_HAS_READ_INVALID."' where `username` = '".$username."' and `state` = '".CMD_TYPE_NO_READ."' and `type` = '".$type."'";
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			} else if ($type == CMD_TYPE_LIMIT_GPRS || $type == CMD_TYPE_LIMIT_VOLUME) {
				if ($username == "all") {
					$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".CMD_TYPE_HAS_READ_INVALID."' where `state` = '".CMD_TYPE_NO_READ."' and `type` = '".$type."'";
					mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);				
				} else {
					$sql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".CMD_TYPE_HAS_READ_INVALID."' where `username` = '".$username."' and `state` = '".CMD_TYPE_NO_READ."' and `type` = '".$type."'";
					mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);					
				}
			}
		}

		if ($username == "all" && ($type == CMD_TYPE_LIMIT_GPRS || $type == CMD_TYPE_LIMIT_VOLUME)) {
			$listsql = "select * from ".SetDB_USER_TAB($manager);	
			if ($listquery = mysqli_query($link, $listsql)) {
				$listnum_rows = mysqli_num_rows($listquery);
				if ($listnum_rows != 0) {
					while ($row = mysqli_fetch_assoc($listquery)) {
						$listusername = $row["username"];
						$sql = "insert into ".SetDB_CMD_TAB($manager)." (`date`,`username`,`cmd`,`explain`,`state`,`type`,`extrainfo`) values ('".$date."','".$listusername."','".$cmd."','".$explain."','".$state."','".$type."','".$extrainfo."')";
						mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_INSERT);
					}
				}
				mysqli_free_result($listquery);
			} else {
				die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
			}
		} else {
			$sql = "insert into ".SetDB_CMD_TAB($manager)." (`date`,`username`,`cmd`,`explain`,`state`,`type`,`extrainfo`) values ('".$date."','".$username."','".$cmd."','".$explain."','".$state."','".$type."','".$extrainfo."')";
			mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_INSERT);			
		}
		
		echo ECHO_OK;
	} else if ($regflag == FLAG_DEL) {
		if (isset($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
			if ($id == "")
				die (ECHO_NO_ID);	
		} else {
			die (ECHO_NO_ID);	
		}		
		
		$sql = "delete from ".SetDB_CMD_TAB($manager)." where `id` = '".$id."'";
		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
		
		echo ECHO_OK;
	} else if ($regflag == FLAG_RET_SIZE) {
		if (isset($_REQUEST['name'])) {
			$username = $_REQUEST['name'];
			if ($username == "")
				die (ECHO_NO_INPUTNAME);	
		} else {
			die (ECHO_NO_INPUTNAME);	
		}

		$gprslimit = "";
		$audiolimit = "";
		$sql = "select * from ".SetDB_USER_TAB($manager)." where `username` = '".$username."'";
		if ($query = mysqli_query($link, $sql)) {
			$num_rows = mysqli_num_rows($query);
			$showtxt = "machineshowsizestart:";
			
			if ($num_rows == 1) {
				$row = mysqli_fetch_assoc($query);
				$free = $row["free"];
				$all = $row["all"];
				$volume = $row["volume"];
				$gprslimit = $row["gprslimit"];
				$audiolimit = $row["audiolimit"];
				$showtxt .= $free.",,".$all.",,".$volume.",,".$gprslimit.",,".$audiolimit;
			}
			$showtxt .= ":machineshowsizeover";
			echo $showtxt;
			mysqli_free_result($query);
		} else {
			die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		}
	} else if ($regflag == FLAG_GET_VALID_MODE) {
		$sql = "select * from ".SetDB_MODE_TAB($manager)." where `exist` = '1' order by `id`";
		if ($query = mysqli_query($link, $sql)) {
			$num_rows = mysqli_num_rows($query);
			$showmd5 = "";
			
			while ($row = mysqli_fetch_assoc($query)) {
				$id = $row["id"];
				$xmlname = sprintf("mode%03d.xml", $id);
				$xmlsql = "select * from ".SetDB_FILE_TAB($manager)." where `filename` = '".$xmlname."'";
				if ($xmlquery = mysqli_query($link, $xmlsql)) {
					$xmlnum_rows = mysqli_num_rows($xmlquery);		
					if ($xmlnum_rows == 1) {
						$xmlrow = mysqli_fetch_assoc($xmlquery);
						$md5 = $xmlrow["md5"];
						if ($showmd5 != "") {
							$showmd5 .= ";;";
						}				
						$showmd5 .= $id.",,".$md5;
					}
					mysqli_free_result($xmlquery);
				} else {
					die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
				}
			}
			$showtxt = "machineshowvalidmodestart:".$showmd5.":machineshowvalidmodeover";
			echo $showtxt;
			mysqli_free_result($query);
		} else {
			die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		}		
	} else if ($regflag == FLAG_GET_VALID_PLAYLIST) {
		$sql = "select * from ".SetDB_PLAYLIST_TAB($manager)." where `exist` = '1' order by `id`";
		if ($query = mysqli_query($link, $sql)) {
			$num_rows = mysqli_num_rows($query);
			$showmd5 = "";
			
			while ($row = mysqli_fetch_assoc($query)) {
				$id = $row["id"];
				$xmlname = sprintf("playlist%03d.xml", $id);
				$xmlsql = "select * from ".SetDB_FILE_TAB($manager)." where `filename` = '".$xmlname."'";
				if ($xmlquery = mysqli_query($link, $xmlsql)) {
					$xmlnum_rows = mysqli_num_rows($xmlquery);		
					if ($xmlnum_rows == 1) {
						$xmlrow = mysqli_fetch_assoc($xmlquery);
						$md5 = $xmlrow["md5"];
						if ($showmd5 != "") {
							$showmd5 .= ";;";
						}				
						$showmd5 .= $id.",,".$md5;
					}
					mysqli_free_result($xmlquery);
				} else {
					die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
				}
			}
			$showtxt = "machineshowvalidplayliststart:".$showmd5.":machineshowvalidplaylistover";
			echo $showtxt;
			mysqli_free_result($query);
		} else {
			die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		}
	} else if ($regflag == FLAG_GET_SERVERFILE) {
		if (isset($_REQUEST["extra"])) {
			$extra = $_REQUEST["extra"];
			if ($extra == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}
		
		$sql = "select * from ".SetDB_FILE_TAB($manager)." where `extrainfo` = '".$extra."'";
		$sql .= " order by `id` desc";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$showtxt = "machineshowfilestart:";
		$num = 0;
		while ($row = mysqli_fetch_assoc($query)) {
			$filename = $row["filename"];
			$size = $row["size"];
			$md5 = $row["md5"];
			$encrypt = $row["encrypt"];
			$typestr = $row["typestr"];
			$line = $row["line"];
			$limittime = $row["limittime"];
			$quene = $row["quene"];
			$customername = $row["username"];
			$customerinfo = $row["userinfo"];
			$customerlimit = $row["userlimit"];
			$customerpasswd = $row["userpasswd"];
			$videotime = $row["time"];
			if ($num != 0) {
				$showtxt .= ";;";
			}
			$showtxt .= $filename.",,".$size.",,".$md5.",,".$encrypt.",,".$typestr.",,".$line.",,".$limittime.",,".$quene.",,".$customername.",,".$customerinfo.",,".$customerlimit.",,".$customerpasswd.",,".$videotime;
			$num++;
		}
		$showtxt .= ":machineshowfileover";
		echo $showtxt;
		mysqli_free_result($query);	
	} else if ($regflag == FLAG_DEL_LOG) {
		if (isset($_REQUEST["name"])) {
			$username = $_REQUEST["name"];
			if ($username == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}		

		if (isset($_REQUEST["type"])) {
			$type = $_REQUEST["type"];
			if ($type == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}		
		
		if (isset($_REQUEST["start"])) {
			$start = $_REQUEST["start"];
			if ($start == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}	

		if (isset($_REQUEST["end"])) {
			$end = $_REQUEST["end"];
			if ($end == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}	
		
		$sql = "delete from ".SetDB_LOG_TAB($manager, $username)." where `time` >= '".$start."' and `time` <= '".$end."'";		
		if ($type != 0) {
			$sql .= " and `type` = '".$type."'";
		}
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
		echo "ok";		
	} else if ($regflag == FLAG_DOWNLOAD_LOG) {
		if (isset($_REQUEST["name"])) {
			$username = $_REQUEST["name"];
			if ($username == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}		

		if (isset($_REQUEST["type"])) {
			$type = $_REQUEST["type"];
			if ($type == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}		

		if (isset($_REQUEST["start"])) {
			$start = $_REQUEST["start"];
			if ($start == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}	

		if (isset($_REQUEST["end"])) {
			$end = $_REQUEST["end"];
			if ($end == "") {
				die (ECHO_ERROR);
			}
		} else {
			die (ECHO_ERROR);
		}			
		
		$sql = "select * from ".SetDB_LOG_TAB($manager, $username)." where `time` >= '".$start."' and `time` <= '".$end."'";		
		if ($type != 0) {
			$sql .= " and `type` = '".$type."'";
		}
		$sql .= " order by `id`";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		if ($num_rows == 0) {
			echo "machineshowlogstart::machineshowlogover";
			return;
		}
		$timeinfo = iconv("GB2312", "UTF-8//IGNORE", "时间");
		$typeinfo = iconv("GB2312", "UTF-8//IGNORE", "类型");
		$contextinfo = iconv("GB2312", "UTF-8//IGNORE", "内容");		
		$showtxt = "machineshowlogstart:<table border=1><tr><td>".$timeinfo."</td><td>".$typeinfo."</td><td>".$contextinfo."</td></tr>";
		while ($row = mysqli_fetch_assoc($query)) {
			$time = $row["time"];
			$showtime = substr($time, 0, 4)."-".substr($time, 4, 2)."-".substr($time, 6, 2)." ".substr($time, 8, 2).":".substr($time, 10, 2).":".substr($time, 12, 2);
			$type = $row["type"];
			$context = $row["name"];
			$showtxt .= "<tr><td>".$showtime."</td><td>".SetLogType($type)."</td><td>".$context."</tr>";
		}
		$showtxt .= "</table>:machineshowlogover";
		echo $showtxt;
		mysqli_free_result($query);		
	} else if ($regflag == FLAG_TERMINAL_FILE) {
		if (isset($_REQUEST['name'])) {
			$username = $_REQUEST['name'];
			if ($username == "")
				die (ECHO_NO_INPUTNAME);	
		} else {
			die (ECHO_NO_INPUTNAME);	
		}
				
		$sql = "select * from ".SetDB_USER_TAB($manager)." where `username` = '".$username."'";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$showtxt = "machineshowfilestart:";
		if ($num_rows == 1) {
			$row = mysqli_fetch_assoc($query);	
			$terminalfile = $row["terminalfile"];
			$showtxt .= $terminalfile;
		}
		$showtxt .= ":machineshowfileover";
		echo $showtxt;
		mysqli_free_result($query);
	} else if ($regflag == FLAG_CHECK_SERVERFILE) {
		$sql = "select * from ".SetDB_FILE_TAB($manager)." where `extrainfo` = '0'";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$path = "./upload/".md5($manager.DB_PWD.$manager)."/";
		$del = false; 
		
		while ($row = mysqli_fetch_assoc($query)) {
			$filename = $row["filename"];
			$md5 = $row["md5"];
			if (substr($filename, -4) == ".xml" && (substr($filename, 0, 3) == "gps" || substr($filename, 0, 5) == "clock"))
				continue;
			$filename = $path.$filename;
			if (!file_exists($filename)) {
				$sql = "delete from ".SetDB_FILE_TAB($manager)." where `extrainfo` = '0' and `md5` = '".$md5."'";
				$delquery = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
				$del = true;
			}
		}
		if ($del == true)
			echo "del";
		echo ECHO_OK;
		mysqli_free_result($query);
	} else if ($regflag == FLAG_GET_ALARM) {
		if (isset($_REQUEST['name'])) {
			$username = $_REQUEST['name'];
			if ($username == "")
				die (ECHO_NO_INPUTNAME);	
		} else {
			die (ECHO_NO_INPUTNAME);	
		}

		$sql = "select * from ".SetDB_USER_TAB($manager)." where `username` = '".$username."'";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$showtxt = "machineshowalarmstart:";
		
		if ($num_rows == 1) {
			$row = mysqli_fetch_assoc($query);
			$alarm = $row["alarm"];
			if ($alarm == "")
				$alarm = "000000000000000000000000000000000000000000000000";
			$showtxt .= $alarm;
		}
		$showtxt .= ":machineshowalarmover";
		echo $showtxt;
		mysqli_free_result($query);
	} else if ($regflag == FLAG_GET_VALID_GROUPLIST) {
		$sql = "select * from ".SetDB_FILE_TAB($manager);
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$showtxt = "machineshowvalidgroupliststart:";
		$fileinfo = "";
		
		while ($row = mysqli_fetch_assoc($query)) {
			$filename = $row["filename"];
			$md5 = $row["md5"];
			if (substr($filename, 0, 5) == "list-" && substr($filename, -4) == ".xml") {
				if ($filename == "list-total.xml") {
					$groupnum_rows = 1;
				} else {
					$groupname = substr($filename, 5, strlen($filename) - 9);
					$groupsql = "select * from ".SetDB_USER_TAB($manager)." where `groupname` = '".$groupname."'";
					$groupquery = mysqli_query($link, $groupsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
					$groupnum_rows = mysqli_num_rows($groupquery);
				}
				//多余的那些组，不理会，即以前存在后来被删除
				if ($groupnum_rows != 0) {
					if ($fileinfo != "") {
						$fileinfo .= ";;";
					}
					$fileinfo .= $filename.",,".$md5;
				}
			}
		}
		$showtxt .= $fileinfo.":machineshowvalidgrouplistover";
		echo $showtxt;
		mysqli_free_result($query);		
	} else if ($regflag == FLAG_GET_SERVER_STATE) {
		$showtxt = "serveronlinestart:";
		$now = time() - 90;
		$sql = "select * from ".SetDB_SERVER_TAB($manager);
		$query = mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num = 0;
    while ($row = mysqli_fetch_assoc($query)) {
			$servername = $row["servername"];
			$alias = $row["alias"];
			$remarks = $row["remarks"];
			$online_time = $row["online_time"];
			$online = 0;
			if ($online_time >= $now)
				$online = 1;
			$state = $row["state"];
			$list = $row["list"];
			$serverip = $row["serverip"];
			$servermask = $row["mask"];
			$servergateway = $row["gateway"];
			$version = $row["version"];
			$err = $row["err"];
			$info = $row["info"];
			$lastofftime = $row["lastofftime"];
			$lastontime = $row["lastontime"];
			$restartcount = $row["restartcount"];
			if ($num != 0) {
				$showtxt .= ";;";
			}
			$showtxt .= $servername.",,".$alias.",,".$online.",,".$version.",,".$state.",,".$remarks.",,".$list.",,".$serverip.",,".$servermask.",,".$servergateway.",,".$err.",,".$info;
			if ($online == 0) {
				if ($online_time == "") {
					$lastofftime = "";
				} else {
					$lastofftime = date('Y-m-d H:i:s', $online_time);
				}
				$lastontime = "";
			} else { 
				if ($lastofftime != "") {
					$lastofftime = date('Y-m-d H:i:s', $lastofftime);			
				}
				if ($lastontime != "") {
					$lastontime = date('Y-m-d H:i:s', $lastontime);			
				}
			}
			$showtxt .= ",,".$lastofftime.",,".$lastontime.",,".$restartcount;
			$num++;
		}
		
		$showtxt .= ":serveronlineover";
		echo $showtxt;
		mysqli_free_result($query);
	} else if ($regflag == FLAG_CHANGE_SERVER_INFO) {
		if (isset($_REQUEST["mac"])) {
			$servername = $_REQUEST["mac"];
			if ($servername == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}		
	
		if (isset($_REQUEST["alias"])) {
			$alias = $_REQUEST["alias"];
		}		

		if (isset($_REQUEST["remarks"])) {
			$remarks = $_REQUEST["remarks"];
		}
		
		$sql = "update ".SetDB_SERVER_TAB($manager)." set `alias` = '".$alias."', `remarks` = '".$remarks."'";
		if (isset($_REQUEST["list"])) {
			$list = $_REQUEST["list"];
			$sql .= ", `list` = '".$list."', `replaceflag` = '1'";
		}
		$sql .= " where `servername` = '".$servername."'";

		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
		echo "ok";		
	} else if ($regflag == FLAG_DEL_SERVER_INFO) {
		if (isset($_REQUEST["mac"])) {
			$servername = $_REQUEST["mac"];
			if ($servername == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}
		
		$sql = "delete from ".SetDB_SERVER_TAB($manager)." where `servername` = '".$servername."'";
		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
		echo "ok";		
	} else if ($regflag == FLAG_SET_FILE) {
		if (isset($_REQUEST["name"])) {
			$filename = $_REQUEST["name"];
			if ($filename == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}
		$sql = "update ".SetDB_FILE_TAB($manager)." set ";
		if (isset($_REQUEST["typestr"])) {
			$typestr = $_REQUEST["typestr"];
			if ($typestr == "") {
				die (ECHO_ERROR);			
			}
			$sql .= "`typestr` = '".$typestr."'";
		}

		if (isset($_REQUEST["line"])) {
			$line = $_REQUEST["line"];
			if (substr($sql, -1) == "'")
				$sql .= ", ";
			$sql .= "`line` = '".$line."'";
		}

		if (isset($_REQUEST["limittime"])) {
			$limittime = $_REQUEST["limittime"];
			if (substr($sql, -1) == "'")
				$sql .= ", ";
			$sql .= "`limittime` = '".$limittime."'";
		}			
		
		if (substr($sql, -1) != "'")
			die (ECHO_ERROR);
		$sql .= " where `filename` = '".$filename."'";
		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
		echo "ok";	
	} else if ($regflag == FLAG_SET_QUENE) {
		if (isset($_REQUEST["flag"])) {
			$typeflag = $_REQUEST["flag"];
			if ($typeflag == "") {
				die (ECHO_ERROR);			
			}
			if ($typeflag == 0)
				$typestr = "节目";
			else if ($typeflag == 1)
				$typestr = "广告";
		} else {
			die (ECHO_ERROR);
		}
		
		if (isset($_REQUEST["quene"])) {
			$quenestr = $_REQUEST["quene"];
		} else {
			die (ECHO_ERROR);
		}
		
		if ($typeflag == 0) {
			$sql = "truncate table ".SetDB_QUENE_TAB($manager);
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
			if ($quenestr != "") {
				$singelcontext = strtok($quenestr, ";;");
				while ($singelcontext != false) {
					$commaloc = strpos($singelcontext, ",,");
					if ($commaloc == false) {
						break;
					}
					$filename = substr($singelcontext, 0, $commaloc);
					$remainstr = substr($singelcontext, $commaloc + 2);
					$commaloc = strpos($remainstr, ",,");				
					$line = substr($remainstr, 0, $commaloc);
					$quene = substr($remainstr, $commaloc + 2);
	
					$sql = "insert into ".SetDB_QUENE_TAB($manager)." (`filename`,`line`,`quene`) values ('".$filename."','".$line."','".$quene."')";
					mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
					$singelcontext = strtok(";;");
				}
			}
		} else if ($typeflag == 1) {
			$sql = "update ".SetDB_FILE_TAB($manager)." set `quene` = '0' where `typestr` = '".$typestr."'";
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			if ($quenestr != "") {
				$singelcontext = strtok($quenestr, ";;");
				while ($singelcontext != false) {
					$commaloc = strpos($singelcontext, ",,");
					if ($commaloc == false) {
						break;
					}
					$filename = substr($singelcontext, 0, $commaloc);
					$filequene = substr($singelcontext, $commaloc + 2);
	
					$sql = "update ".SetDB_FILE_TAB($manager)." set `quene` = '".$filequene."' where `typestr` = '".$typestr."' and `filename` = '".$filename."'";
					mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
					$singelcontext = strtok(";;");
				}
			}
			$sql = "update ".SetDB_FILE_TAB($manager)." set `line` = '0' where `typestr` = '".$typestr."' and `quene` = '0'";
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
		}
		echo "ok";
	} else if ($regflag == FLAG_GET_ACT_QUENE) {
		$sql = "select * from ".SetDB_QUENE_TAB($manager)." order by `quene`";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$showtxt = "machineshowactquenestart:";
		$num = 0;
		
		while ($row = mysqli_fetch_assoc($query)) {          
			$filename = $row["filename"];
			$line = $row["line"];
			if ($num != 0) {
				$showtxt .= ";;";
			}
			$showtxt .= $filename.",,".$line;
			$num++;
		}		
		$showtxt .= ":machineshowactqueneover";
		echo $showtxt;
		mysqli_free_result($query);		
	} else if ($regflag == FLAG_SET_LINE_FLOAT_GPS_FILE) {		
		if (isset($_REQUEST["line"])) {
			$linestr = $_REQUEST["line"];
		} else {
			die (ECHO_ERROR);
		}
		
		$sql = "truncate table ".SetDB_LINE_TAB($manager);
		mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);
		if ($linestr != "") {
			$singelcontext = strtok($linestr, ";;");
			while ($singelcontext != false) {
				$commaloc = strpos($singelcontext, ",,");
				if ($commaloc == false) {
					break;
				}
				$groupname = substr($singelcontext, 0, $commaloc);
				$remainstr = substr($singelcontext, $commaloc + 2);
				$commaloc = strpos($remainstr, ",,");				
				$textfile = substr($remainstr, 0, $commaloc);
				$gpsfile = substr($remainstr, $commaloc + 2);

				$sql = "insert into ".SetDB_LINE_TAB($manager)." (`groupname`,`textfile`,`gpsfile`) values ('".$groupname."','".$textfile."','".$gpsfile."')";
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
				$singelcontext = strtok(";;");
			}
		}
		echo "ok";
	} else if ($regflag == FLAG_GET_LINE_FLOAT_GPS_FILE) {
		$sql = "select * from ".SetDB_LINE_TAB($manager);
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		$showtxt = "machineshowlinetextgpsstart:";
		$num = 0;
		while ($row = mysqli_fetch_assoc($query)) {          
			$groupname = $row["groupname"];
			$textfile = $row["textfile"];
			$gpsfile = $row["gpsfile"];
			if ($num != 0) {
				$showtxt .= ";;";
			}
			$showtxt .= $groupname.",,".$textfile.",,".$gpsfile;
			$num++;
		}		
		$showtxt .= ":machineshowlinetextgpsover";
		echo $showtxt;
		mysqli_free_result($query);	
	} else if ($regflag == FLAG_CHANGE_FILE_USER_INFO_LIMIT) {
		if (isset($_REQUEST["name"]) && isset($_REQUEST["info"]) && isset($_REQUEST["limit"])) {
			$file_username = $_REQUEST["name"];
			$file_userinfo = $_REQUEST["info"];
			$file_userlimit = $_REQUEST["limit"];			
			if ($file_username != "" && $file_userinfo != "" && $file_userlimit != "") {
				$sql = "update ".SetDB_FILE_TAB($manager)." set `userinfo` = '".$file_userinfo."', `userlimit` = '".$file_userlimit."' where `username` = '".$file_username."'";
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
				echo "ok";
			}
		}
	} else if ($regflag == FLAG_SET_DOWNLOAD_TOTAL_LIMIT) {
		if (isset($_REQUEST["num"])) {
			$num = $_REQUEST["num"];
			if ($num == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}
		if ($num == 0)
			$num = 100000;
		$_SESSION["maxdownloadnum"] = $num;
		$sql = "update limit_table set `maxdownload` = '".$num."'";
		mysqli_query($link, $sql) or die("error");
		echo "ok";
	} else if ($regflag == FLAG_GET_DOWNLOAD_TOTAL_LIMIT) {
		$sql = "select * from limit_table";
		$query = mysqli_query($link, $sql) or die("error");
		$row = mysqli_fetch_assoc($query);          
		$limit = $row["maxdownload"];
		mysqli_free_result($query);
		echo "limitstart:".$limit.":limitover";
	} else if ($regflag == FLAG_SET_TIME) {
		if (isset($_REQUEST["name"])) {
			$videoname = $_REQUEST["name"];
			if ($videoname == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}
		if (isset($_REQUEST["time"])) {
			$videotime = $_REQUEST["time"];
			if ($videotime == "") {
				die (ECHO_ERROR);			
			}
		} else {
			die (ECHO_ERROR);
		}
		$sql = "update ".SetDB_FILE_TAB($manager)." set `time` = '".$videotime."' where `filename` = '".$videoname."'";
		echo $sql;
		mysqli_query($link, $sql) or die("error");
		echo "ok";
	}
} else if ($regflag == FLAG_GET || $regflag == FLAG_SET) {
	if (isset($_REQUEST['mode'])) {
		$mode = $_REQUEST['mode'];
		if ($mode == "") {
			die (ECHO_NO_LOGINMODE);
		}		
	} else {
		die (ECHO_NO_LOGINMODE);		
	}

	if (isset($_REQUEST['name'])) {
		$username = $_REQUEST['name'];
		if ($username == "") {
			die (ECHO_NO_INPUTNAME);
		}
		
		if (isset($_REQUEST['managerid'])) {
			$managerid = $_REQUEST['managerid'];
			if ($managerid == "") {
				die (ECHO_NO_MANAGERID);
			}		
		} else {
			die (ECHO_NO_MANAGERID);
		}
		
		$sql = "select * from ".DB_MANAGER_TAB." where `id` = '".$managerid."'";
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		if ($num_rows != 0) {
			$row = mysqli_fetch_assoc($query);
			$manager = $row["managername"];
			
			$sql = "select * from ".SetDB_USER_TAB($manager)." where `username` = '".$username."'";
			$userquery = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
			$num_rows = mysqli_num_rows($userquery);
			if ($num_rows != 0) {
		  	$_SESSION["user"] = true;
		  	$_SESSION["username"] = $username;
		  	$_SESSION["managername"] = $manager;		
			} else {
				echo (ECHO_NO_MACHINE);
			}
		} else {
			echo (ECHO_NO_MANAGERID);
		}
		mysqli_free_result($query);
	} else {
		if (isset($_SESSION["user"]) && $_SESSION["user"] == true) {
			$username = $_SESSION["username"];
			$manager = $_SESSION["managername"];
		} else {
			die (ECHO_NO_MACHINE);
		}
	}

	if (isset($_SESSION["user"]) && $_SESSION["user"] == true) {
		//  当验证通过后，记录在数据库中
		
		$selectsql = "select * from ".SetDB_USER_TAB($manager)." where `username` = '".$username."'";
		$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		
		if ($num_rows == 0) {
			die (ECHO_ERROR);
		}
		
		if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])&&$HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]){
			$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
		} elseif(isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])&&$HTTP_SERVER_VARS["HTTP_CLIENT_IP"]){
			$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
		} elseif(isset($HTTP_SERVER_VARS["REMOTE_ADDR"])&&$HTTP_SERVER_VARS["REMOTE_ADDR"]) {
			$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
		} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		} elseif (getenv("HTTP_CLIENT_IP")) {
			$ip = getenv("HTTP_CLIENT_IP");
		} elseif (getenv("REMOTE_ADDR")) {
			$ip = getenv("REMOTE_ADDR");
		} else {
			$ip = "Unknown";
		}		
		
		$row = mysqli_fetch_assoc($query);
		$last_ip = $row["userip"];
		$last_online_time = $row["online_time"];
		mysqli_free_result($query);
		$now = time();
		$updatesql = "update ".SetDB_USER_TAB($manager)." set `loginmode` = '".$mode."', `online_time` = '".$now."'";
		if ($ip != $last_ip && $ip != "Unknown") {
			$updatesql .= ", `userip` = '".$ip."'";
		}		

		$downloadsql = "update ".DB_DOWNLOAD_TAB." set `onlinetime` = '".$now."' where `manager` = '".$manager."' and `machine` = '".$username."'";
		mysqli_query($link, $downloadsql);
		
		if ($last_online_time < ($now - 300)) {// 5分钟未登录
			$updatesql .= ", `login_time` = '".$now."'";
		}
		if (isset($_REQUEST["free"]) && isset($_REQUEST["all"])) {
			$free = $_REQUEST["free"];
			if ($free == "") {
				die (ECHO_ERROR);
			}					
			$all = $_REQUEST["all"];
			if ($all == "") {
				die (ECHO_ERROR);
			}					
			$updatesql .= ", `free` = '".$free."', `all` = '".$all."'";
		}	  
		if (isset($_REQUEST["volume"])) {
			$volume = $_REQUEST["volume"];
			if ($volume == "") {
				die (ECHO_ERROR);
			}				
			$updatesql .= ", `volume` = '".$volume."'";
		}
		if (isset($_REQUEST["ver"])) {
			$ver = $_REQUEST["ver"];
			if ($ver == "") {
				die (ECHO_ERROR);
			}		
			$updatesql .= ", `ver` = '".$ver."'";
		}
		if (isset($_REQUEST["mcu"])) {
			$mcu = $_REQUEST["mcu"];	
			if ($mcu != "")	
				$updatesql .= ", `mcu` = '".$mcu."'";
			else
				$updatesql .= ", `mcu` = '000000'";
		}			
		if (isset($_REQUEST["alarm"])) {
			$alarm = $_REQUEST["alarm"];
			if ($alarm == "") {
				die (ECHO_ERROR);
			}		
			$updatesql .= ", `alarm` = '".$alarm."'";
		}	
		if (isset($_REQUEST["terminalfile"])) {
			$terminalfile = $_REQUEST["terminalfile"];
			if ($terminalfile == "") {
				die (ECHO_ERROR);
			}		
			$updatesql .= ", `terminalfile` = '".$terminalfile."'";
		}
		if (isset($_REQUEST["playname"])) {
			$playname = $_REQUEST['playname'];
			$updatesql .= ", `playfile` = '".$playname."'";
		}
		if (isset($_REQUEST["gprs"])) {
			$gprs = $_REQUEST["gprs"];
			$updatesql .= ", `extrainfo` = '".$gprs."'";
		}
		
		if (isset($_REQUEST["wifi"])) {
			$wifi = $_REQUEST['wifi'];
			$updatesql .= ", `wifi` = '".$wifi."'";
		}
		if (isset($_REQUEST["speed"])) {
			$speed = $_REQUEST["speed"];
			$updatesql .= ", `downloadspeed` = '".$speed."'";
		}
		if (isset($_REQUEST["net"])) {
			$net = $_REQUEST['net'];
			$updatesql .= ", `network` = '".$net."'";
			if ($net == 0) {
				$updatesql .= ", `isgprson` = '1'";
			}
		}
		
		if (isset($_REQUEST["gps"])) {
			$gps = $_REQUEST['gps'];
			$updatesql .= ", `gps` = '".$gps."'";
		}
		
		if (isset($_REQUEST["expand"])) {
			$expand = $_REQUEST['expand'];
			$updatesql .= ", `expand` = '".$expand."'";
		}		

		if (isset($_REQUEST["ae"])) {
			$error = $_REQUEST['ae'];
			$updatesql .= ", `error` = '".$error."'";
		}
		if (isset($_REQUEST["gl"])) {
			$gl = $_REQUEST['gl'];
			$updatesql .= ", `gprslimit` = '".$gl."'";
		}
		if (isset($_REQUEST["al"])) {
			$al = $_REQUEST['al'];
			$updatesql .= ", `audiolimit` = '".$al."'";
		}		
		
		if ($regflag == FLAG_GET) {			
			$selectsql = "select * from ".SetDB_CMD_TAB($manager)." where `username` = '".$username."' and (`state` <= '".URL_TYPE_NO_READ."' or `state` = '".URL_TYPE_HAS_READ_DOWNLOADING."') order by `id`";
			$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);	
			$num_rows = mysqli_num_rows($query);
			$showtxt = "cmdstart:";
			if ($num_rows != 0) {
				$priorflag = 0;
				while ($row = mysqli_fetch_assoc($query)) {
					$cmd = $row["cmd"];
					if ($cmd == "cmd:1") {//开机重启，优先处理
						$priorflag = 1;
						break;
					} else if (strlen($cmd) > 6 && substr($cmd, 0, 6) == "cmd:4:") {//设置音量，优先处理
						$priorflag = 1;
						break;
					} else if ($cmd == "cmd:dellog") {//删除日志，优先处理
						$priorflag = 1;
						break;
					} else if ($cmd == "cmd:3g") {//设置3g状态，优先处理
						$priorflag = 1;
						break;
					}
				}
				if ($priorflag == 0) {
					mysqli_free_result($query);
					$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
					$row = mysqli_fetch_assoc($query);
				} 
				$id = $row["id"];
				$cmd = $row["cmd"];
				$state = $row["state"];
				$extrainfo = $row["extrainfo"];
				if ($extrainfo == "") {
					$extrainfo = "0";
				}
				$showtxt .= $id.",,".$cmd.",,".$state.",,".$extrainfo;
				mysqli_free_result($query);
			} else {
				$updatesql .= ", `downloadper` = '".FILE_ALL_OVER."'";
				$sql = "delete from ".DB_DOWNLOAD_TAB." where `manager` = '".$manager."' and`machine` = '".$username."'";
				mysqli_query($link, $sql);
			}
			$showtxt .= ":cmdover";
			echo $showtxt;
			if (isset($_REQUEST['time'])) {
				$machinetime = $_REQUEST['time'];
				$servertime = time();
				if ($machinetime > $servertime) {
					if ($machinetime > ($servertime + 60)) {
						$nowtime = date('Y-w-m-d-H-i-s', $servertime);
						echo $nowtime;
					}
				} else if ($machinetime < $servertime) {
					if (($machinetime + 60) < $servertime) {
						$nowtime = date('Y-w-m-d-H-i-s', $servertime);
						echo $nowtime;
					}
				}
			}
		} else if ($regflag == FLAG_SET) {
			$cmdidorname = 0;
			$id = "";
			$downloadname = "";
			if (isset($_REQUEST['id'])) {
				$id = $_REQUEST['id'];
				if ($id == "") {
					die (ECHO_NO_ID);
				}
				$cmdidorname = 1;
			} else if (isset($_REQUEST['downloadname'])) {
				$downloadname = $_REQUEST['downloadname'];
				if ($downloadname == "") {
					die (ECHO_NO_ID);
				}
				$cmdidorname = 2;
			}
	
			if ($cmdidorname == 0) {
				die (ECHO_NO_ID);
			} else if ($cmdidorname == 1) {
				$selectsql = "select * from ".SetDB_CMD_TAB($manager)." where `id` = '".$id."' and `state` != '".CMD_TYPE_NO_READ."' and `state` != '".CMD_TYPE_HAS_READ."' and `state` != '".CMD_TYPE_HAS_READ_INVALID."'";
				$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
				$num_rows = mysqli_num_rows($query);
				if ($num_rows != 0) {
					$row = mysqli_fetch_assoc($query);
					$explain = $row["explain"];
					$start = strpos($explain, "\"");
					$end = strrpos($explain, "\"");
					if ($start != false && $end != false && $start != $end) {
						$downloadname = substr($explain, $start + 1, $end - $start - 1);
						$downloadname = iconv("gb2312", "UTF-8", $downloadname);
					}
				}
				mysqli_free_result($query);
			} else if ($cmdidorname == 2) {
				if (substr($downloadname, -4) == ".xml") {
					$selectsql = "select * from ".SetDB_CMD_TAB($manager)." where `username` = '".$username."' and (`state` = '".URL_TYPE_NO_READ."' or `state` = '".URL_TYPE_HAS_READ_DOWNLOADING."') and `type` = '";
					if (substr($downloadname, 0, 4) == "mode") {
						$selectsql .= FILE_TYPE_MODEXML."'";
					} else if (substr($downloadname, 0, 8) == "playlist") {
						$selectsql .= FILE_TYPE_PLAYLISTXML."'";
					} else {
						die (ECHO_ERROR);
					}
					$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);	
					$num_rows = mysqli_num_rows($query);
					if ($num_rows != 0) {
						$row = mysqli_fetch_assoc($query);
						$id = $row["id"];
					}
					mysqli_free_result($query);
				}
			}	
			
			if (isset($_REQUEST['state'])) {
				$state = $_REQUEST['state'];
				$downloadper = "";
				if ($state == CMD_TYPE_NO_READ) {
					$state = CMD_TYPE_HAS_READ;
				}	else if ($state == URL_TYPE_NO_READ) {
					$state = URL_TYPE_HAS_READ_NO_DEL;
				}	else if ($state == URL_TYPE_HAS_READ_EXIST) {
					//$state = URL_TYPE_HAS_READ_EXIST;
					$downloadper = FILE_EXISTS;
				}	else if ($state == URL_TYPE_HAS_READ_DOWNLOADING) {
					//$state = URL_TYPE_HAS_READ_DOWNLOADING;
					if (isset($_REQUEST['extra'])) {
						$downloadper = $_REQUEST['extra'];
					} else {
						die (ECHO_NO_DOWNLOADPERCENT);
					}
				}	else if ($state == URL_TYPE_HAS_READ_DOWNLOADOVER) {
					//$state = URL_TYPE_HAS_READ_DOWNLOADOVER;
					$downloadper = FILE_SINGLE_OVER;
				} else if ($state == URL_TYPE_HAS_READ_NOT_FOUND) {
					//$state = URL_TYPE_HAS_READ_NOT_FOUND;
					$downloadper = FILE_NOT_EXISTS;
				} else if ($state == URL_TYPE_HAS_READ_WAITING) {
					//$state = URL_TYPE_HAS_READ_WAITING;
					$downloadper = FILE_WAITING;
				} else {
					die (ECHO_NO_STATE);
				}
			} else {
				die (ECHO_NO_STATE);
			}
	
			$cmdsql = "update ".SetDB_CMD_TAB($manager)." set `state` = '".$state."' where `id` = '".$id."'";
			mysqli_query($link, $cmdsql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			
			if ($downloadname != "") {
				$updatesql .= ", `downloadfile` = '".$downloadname."', `downloadper` = '".$downloadper."'";		
			}		
					
			echo ECHO_OK;
			if ($cmdidorname == 2) {
				$selectsql = "select * from ".SetDB_CMD_TAB($manager)." where `username` = '".$username."' and `state` = '".CMD_TYPE_NO_READ."' order by `id`";
				$query = mysqli_query($link, $selectsql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);	
				$num_rows = mysqli_num_rows($query);
	
				if ($num_rows != 0) {
					$showtxt = "cmdstart:";
					$priorflag = 0;
					while ($row = mysqli_fetch_assoc($query)) {
						$cmd = $row["cmd"];
						if ($cmd == "cmd:1") {//开机重启，优先处理
							$priorflag = 1;
							break;
						} else if (strlen($cmd) > 6 && substr($cmd, 0, 6) == "cmd:4:") {//设置音量，优先处理
							$priorflag = 1;
							break;
						} else if ($cmd == "cmd:dellog") {//删除日志，优先处理
							$priorflag = 1;
							break;
						} else if ($cmd == "cmd:3g") {//设置3g状态，优先处理
							$priorflag = 1;
							break;
						}
					}

					if ($priorflag == 1) {
						$id = $row["id"];
						$cmd = $row["cmd"];
						$state = $row["state"];
						$extrainfo = $row["extrainfo"];
						if ($extrainfo == "") {
							$extrainfo = "0";
						}
						$showtxt .= $id.",,".$cmd.",,".$state.",,".$extrainfo.":cmdover";	
						echo $showtxt;
					}
					mysqli_free_result($query);
				}
			}
		}
		$updatesql .= " where `username` = '".$username."'";
		mysqli_query($link, $updatesql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);//更新用户的最后在线时间，容量，声音大小，版本等
	}
} else if ($regflag == FLAG_SET_SERVER_STATE) {	
	if (isset($_REQUEST["managerid"])) {
		$managerid = $_REQUEST["managerid"];
		if ($managerid == "") {
			die (ECHO_NO_ID);			
		}
	} else {
		die (ECHO_NO_ID);
	}	
	
	if (isset($_REQUEST["mac"])) {
		$servername = $_REQUEST["mac"];
		if ($servername == "") {
			die (ECHO_ERROR);			
		}
	} else {
		die (ECHO_ERROR);
	}		

	if (isset($_REQUEST["s"])) {
		$state = $_REQUEST["s"];
		if ($state == "") {
			die (ECHO_ERROR);			
		}
	} else {
		die (ECHO_ERROR);
	}
		
	$sql = "select * from ".DB_MANAGER_TAB." where `id` = '".$managerid."'";
	$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
	$num_rows = mysqli_num_rows($query);
	if ($num_rows == 0) {
		die (ECHO_NO_ID);
	}
	$row = mysqli_fetch_assoc($query);
	$manager = $row["managername"];
	mysqli_free_result($query);
	
	$now = time();
	$restartflag = 0; 
	$restartcount = 0; 
	if ($state == 0 || $state == 1) {
		$sql = "select * from ".SetDB_SERVER_TAB($manager)." where `servername` = '".$servername."'";	
		$query = mysqli_query($link, $sql) or die (ECHO_MYSQL_ERROR_QUERY_TABLE_TO_FIND);
		$num_rows = mysqli_num_rows($query);
		if ($num_rows != 0) {
			$row = mysqli_fetch_assoc($query);
			$lastofftime = $row["online_time"];
			$laststate = $row["state"];
			$restartcount = $row["restartcount"];
			if ($restartcount == "")
				$restartcount = 0;
			if (($state == 0 && $laststate != 0) || ($state == 1 && $laststate == 2) || ($now - $lastofftime) > 60) {
				$restartflag = 1;
				$restartcount++;
			}
			mysqli_free_result($query);
		}
	}

	$sql = "update ".SetDB_SERVER_TAB($manager)." set `online_time` = '".$now."', `state` = '".$state;
	if ($restartflag == 1) {
		$sql .= "', `lastofftime` = '".$lastofftime."', `lastontime` = '".$now."', `restartcount` = '".$restartcount;
	}
	
	if (isset($_REQUEST["v"])) {
		$version = $_REQUEST["v"];
		if ($version == "") {
			$version = "0";			
		}
		$sql .= "', `version` = '".$version;
	}	
	if (isset($_REQUEST["ip"])) {
		$serverip = $_REQUEST["ip"];
		if ($serverip == "") {
			die (ECHO_ERROR);			
		}
		$sql .= "', `serverip` = '".$serverip;
	}
	if (isset($_REQUEST["mask"])) {
		$servermask = $_REQUEST["mask"];
		if ($servermask == "") {
			die (ECHO_ERROR);			
		}
		$sql .= "', `mask` = '".$servermask;
	}
	if (isset($_REQUEST["gate"])) {
		$gateway = $_REQUEST["gate"];
		if ($gateway == "") {
			die (ECHO_ERROR);			
		}
		$sql .= "', `gateway` = '".$gateway;
	}
	if (isset($_REQUEST["err"])) {
		$error = $_REQUEST["err"];
		$sql .= "', `err` = '".$error;
	}
	if (isset($_REQUEST["info"])) {
		$info = $_REQUEST["info"];
		$sql .= "', `info` = '".$info;
	}	
	$sql .= "' where `servername` = '".$servername."'";
	mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
	echo "ok";
} else {
	die (ECHO_NO_REGFLAG);
}
mysqli_close($link);
?>
